package co.fitstart.admin.web.controller.user.distributor;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.service.DistributionRecordService;
import co.fitstart.admin.service.DistributorService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.view.ExcelTemplateView;
import co.fitstart.entity.user.distributor.DistributionRecord;
import co.fitstart.entity.user.distributor.DistributionRecordType;

@Controller
@RequestMapping("/distribution-records")
@MenuSetting("menu-user")
public class DistributionRecordController {
    
    private static final Logger logger = LoggerFactory.getLogger(DistributionRecordController.class);
    
    @Autowired
    private DistributionRecordService distributionRecordService;
    
    @Autowired
    private DistributorService distributorService;
    
    /**
     * 
     * @param type
     * @param page
     * @param request
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-distribution-record-management")
    public ModelAndView list(@RequestParam(value = "type", required = false, defaultValue = "withdrawing") String type,
                             Page page,
                             HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("user/distributor/distribution-record/distribution-record-list");
        
        List<DistributionRecord> records = distributionRecordService.listByType(DistributionRecordType.aliasOf(type), page);
        modelAndView.addObject("records", records);
        
        modelAndView.addObject("type", type);
        
        modelAndView.addObject("page", page);
        
        modelAndView.addObject("refer", "list");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        return modelAndView;
    }
    
    /**
     * 
     * @param distributionRecordId
     * @return
     */
    @RequestMapping(value = "/{distributionRecordId}/pay", method = RequestMethod.GET)
    public ModelAndView pay(@PathVariable("distributionRecordId") Long distributionRecordId) {
        ModelAndView modelAndView = new ModelAndView("user/distributor/distribution-record/distribution-record-pay");
        
        modelAndView.addObject("distributionRecordId", distributionRecordId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param agentId
     * @param agentRecordId
     * @return
     */
    @RequestMapping(value = "/{distributionRecordId}/pay", method = RequestMethod.PATCH)
    public @ResponseBody String onPay(@PathVariable("distributionRecordId") Long distributionRecordId) {
        Result result = new Result();
        
        try {
            
            DistributionRecord distributionRecord = distributionRecordService.load(distributionRecordId);
            
            distributorService.payDistribution(distributionRecord.getDistributor(), distributionRecord);
            
            result.success().message("明星代言人打款成功");
            
        } catch (Exception e) {
            
            logger.error("onPay() - ", e);
            result.fail().message("明星代言人打款失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param type
     * @return
     */
    @RequestMapping(value = "/export-view", method = RequestMethod.GET)
    public ModelAndView export(@RequestParam("type") String type) {
        ModelAndView modelAndView = new ModelAndView("user/distributor/distribution-record/distribution-record-export");
        
        modelAndView.addObject("type", type);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param type
     * @param beginTime
     * @param endTime
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView onExport(@RequestParam("type") String type,
                                 @RequestParam("beginTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date beginTime,
                                 @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("distribution-record.xls"));
        
        List<DistributionRecord> distributionRecords = distributionRecordService.listByTime(DistributionRecordType.aliasOf(type), beginTime, endTime);
        
        modelAndView.addObject("distributionRecords", distributionRecords);
        
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
    }


}
